package fun.cyhgraph.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import fun.cyhgraph.annotation.AuthCheck;
import fun.cyhgraph.common.UserConstant;
import fun.cyhgraph.model.entity.MerchantInfo;
import fun.cyhgraph.model.vo.ListMerchantVO;
import fun.cyhgraph.result.Result;
import fun.cyhgraph.service.MerchantInfoService;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.sql.Time;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController("userShopController") // 重命名，防止两个端admin、user的ShopController冲突
@RequestMapping("/user/shop")
@Tag(name = "店铺营业状态管理")
@Slf4j
public class ShopController {

    @Resource
    private MerchantInfoService merchantInfoService;

//    public static final String KEY = "SHOP_STATUS";
    @Operation(summary = "获取状态(始终为营业)")
    @GetMapping("/status")
    public Result<Integer> getStatus(Integer merchantId){
//        Integer status =  (Integer)redisTemplate.opsForValue().get(KEY);

    return Result.success(1);
    }
    @Operation(summary = "根据店铺id获取对应的营业状态")

    @GetMapping("/statusForMerchant")
    public Result<Integer> getStatusForMerchant(Integer merchantId){
//        Integer status =  (Integer)redisTemplate.opsForValue().get(KEY);
        LambdaQueryWrapper<MerchantInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MerchantInfo::getMerchantId, merchantId);
        MerchantInfo merchantInfo = merchantInfoService.getOne(queryWrapper);
        if (merchantInfo == null){
            return Result.error("商家信息不存在");
        }
        Integer status;
        Time openTime = merchantInfo.getOpenTime();
        Time closeTime = merchantInfo.getCloseTime();
        if(merchantInfo.getMerchantStatus() != 0){
            status = merchantInfo.getMerchantStatus();
            return Result.success(status);
        }
        // 根据当前时间判断营业状态
        if (openTime != null && closeTime != null) {
            if (LocalDateTime.now().toLocalTime().isAfter(openTime.toLocalTime()) && LocalDateTime.now().toLocalTime().isBefore(closeTime.toLocalTime())) {
                status = 1;
            } else {
                status = 0;
            }
            return Result.success(status);
        }else{
            return Result.error("可能没有对应店铺的信息");
        }

    }
    @AuthCheck(mustRole = UserConstant.ADMIN_OR_MERCHANT_ROLE)
    @Operation(summary = "手动设置店铺营业状态")
    @PutMapping("/{status}")
    public Result setStatus(@PathVariable Integer status , @RequestParam(required = false) Integer merchantId){
        merchantInfoService.judgeMerchantInfo(merchantId);
        MerchantInfo merchantInfo = new MerchantInfo();
        merchantInfo.setMerchantStatus(status);
        boolean b = merchantInfoService.updateById(merchantInfo);
        if (!b){
            return Result.error("设置失败");
        }
        return Result.success();
    }
    /**
     * 展示所有商家
     */
    @Operation(summary = "获取所有商家信息")
    @GetMapping("/listByMerchant")
    public Result<List<ListMerchantVO>> getDishListByMerchantId() {
        List<MerchantInfo> list = merchantInfoService.list();
        List<ListMerchantVO> listMerchantVOS = new ArrayList<>();
        list.stream().forEach(x -> {
            ListMerchantVO listMerchantVO = new ListMerchantVO(x.getId(), x.getAddress(), x.getDescription(), x.getMerchantPhone());
            listMerchantVOS.add(listMerchantVO);
        });
        return Result.success(listMerchantVOS);
    }
}
